package com.stars.services.impl.quartz;

import com.stars.common.WheterYN;
import com.stars.common.base.BaseConstant;
import com.stars.common.enums.JobOperateEnum;
import com.stars.common.valid.MyException;
import com.stars.entity.job.MyScheduleJobEntity;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.lang.reflect.Method;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-10-12 09:47
 * @note
 */
public class QuartzFactory implements Job {


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取调度数据
        MyScheduleJobEntity myJobEntity = (MyScheduleJobEntity) jobExecutionContext.getMergedJobDataMap().get("scheduleJob");
        //如果运行的。
        if(WheterYN.YES.getValue().equals(myJobEntity.getIsNowRun())){
            //获取对应的Bean
            Object object = SpringUtil.getBean(myJobEntity.getClazzPath());

            try {
                //无参数
                if(StringUtils.isBlank(myJobEntity.getParams())){
                    Method method = object.getClass().getMethod(myJobEntity.getJobMethod());
                    method.invoke(object);
                //有参
                }else {
                    //利用反射执行对应方法
                    Method method = object.getClass().getMethod(myJobEntity.getJobMethod(),String.class);
                    method.invoke(object,myJobEntity.getParams());
                }

            } catch (Exception e) {
               throw new MyException("执行定时任务失败");
            }
        }

    }
}
